package com.hulk.dryad.manage.framework.websocket.server.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hulk.dryad.manage.framework.websocket.server.handshake.DefaultWebSocketPrincipalResolve;
import com.hulk.dryad.manage.framework.websocket.server.handshake.PrincipalHandshakeHandler;
import com.hulk.dryad.manage.framework.websocket.server.handshake.WebSocketAuthHandshakeInterceptor;
import com.hulk.dryad.manage.framework.websocket.server.handshake.WebSocketPrincipalResolve;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;


/**
 * websocket 配置
 *
 * @author hulk
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(
		name = "enabled",
		prefix = WebSocketServerProperties.PREFIX,
		havingValue = "true"
)
@EnableWebSocketMessageBroker
@EnableConfigurationProperties(WebSocketServerProperties.class)
public class WebSocketServerConfiguration {



	@Bean
	public PrincipalHandshakeHandler handshakeHandler() {
		return new PrincipalHandshakeHandler();
	}

	@Bean
	public WebSocketAuthHandshakeInterceptor authHandshakeInterceptor(ObjectProvider<WebSocketPrincipalResolve> provider,
																	  WebSocketServerProperties properties) {
		WebSocketPrincipalResolve principalResolve = provider.getIfAvailable(() -> new DefaultWebSocketPrincipalResolve());
		return new WebSocketAuthHandshakeInterceptor(principalResolve, properties);
	}

	@Bean
	public WebSocketServerConfigurer webSocketConfigurer(PrincipalHandshakeHandler handshakeHandler,
														 WebSocketAuthHandshakeInterceptor authHandshakeInterceptor,
														 ObjectMapper objectMapper,
														 WebSocketServerProperties properties) {
		WebSocketServerConfigurer webSocketServerConfigurer = new WebSocketServerConfigurer(handshakeHandler, authHandshakeInterceptor, objectMapper, properties);
		return webSocketServerConfigurer;
	}

//	@EventListener
//	public void onSocketConnected(SessionConnectedEvent event) {
//		StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
//		log.info("WebSocket SessionId: {} Connected: {}", sha.getSessionId(), event.getMessage());
//	}
//
//	@EventListener
//	public void onSocketDisconnected(SessionDisconnectEvent event) {
//		StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
//		log.info("WebSocket SessionId: {} Disconnected: {}", sha.getSessionId(), event.getMessage());
//	}

}
